package com.jsmy.marketing.activity;

import android.os.Build;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsmy.marketing.R;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.base.BaseActivity;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.utils.ShareUtil;
import com.jsmy.marketing.utils.ToastUtil;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * 资料详情
 * @return
 */
public class ResouceDetailActivity extends BaseActivity implements UMShareListener{
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.m_webview)
    WebView mWebview;

    public String id;
    public String url;
    public String title;
    public String note;
    public String share;

    String sharePt;
    @BindView(R.id.img_share)
    ImageView imgShare;

    @Override
    protected int resId() {
        return R.layout.activity_resouce_detail;
    }


    @Override
    protected void initData() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ResouceDetailActivity.this.finish();
            }
        });
        id=getPrevExtras().getString("id");
        url=getPrevExtras().getString("url");
        title=getPrevExtras().getString("title");
        note=getPrevExtras().getString("note");
        share=getPrevExtras().getString("share");
        tvContent.setText(title);

        imgShare.setVisibility(View.VISIBLE);
        imgShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (share.equals("Y")){
                    ShareUtil.shareWeb(ResouceDetailActivity.this, url, title, note, ResouceDetailActivity.this);
                }else {
                    ShareUtil.shareWeb(ResouceDetailActivity.this, url + com.jsmy.marketing.manager.UserManager.getInstance().getId(), title, note, ResouceDetailActivity.this);
                }

            }
        });


        mWebview.setVerticalScrollbarOverlay(true); //指定的垂直滚动条有叠加样式
        WebSettings settings = mWebview.getSettings();
        settings.setUseWideViewPort(true);//设定支持viewport
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);//设定支持缩放
        settings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebview.clearHistory();
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        if (share.equals("Y")){
            mWebview.loadUrl("" + url);
        }else {
            mWebview.loadUrl("" + url + com.jsmy.marketing.manager.UserManager.getInstance().getId());
        }

        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ResouceDetailActivity.this.finish();
            }
        });

        tvContent.setText(title);

        imgShare.setVisibility(View.VISIBLE);
        imgShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (share.equals("Y")){
                    ShareUtil.shareWeb(ResouceDetailActivity.this, url, title, note, ResouceDetailActivity.this);
                }else {
                    ShareUtil.shareWeb(ResouceDetailActivity.this, url + com.jsmy.marketing.manager.UserManager.getInstance().getId(), title, note, ResouceDetailActivity.this);
                }

            }
        });

        mWebview.setVerticalScrollbarOverlay(true); //指定的垂直滚动条有叠加样式
        settings = mWebview.getSettings();
        settings.setUseWideViewPort(true);//设定支持viewport
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);//设定支持缩放
        settings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebview.clearHistory();
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        if (share.equals("Y")){
            mWebview.loadUrl("" + url);
        }else {
            mWebview.loadUrl("" + url + com.jsmy.marketing.manager.UserManager.getInstance().getId());
        }


    }

    @Override
    public void refreshData(Object response, String position) {
        switch (position){
            case Link.APP_SAVE_FX_INFO:
                ToastUtil.showShort(this, "分享成功！");
                break;
                default:
        }
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA platform) {
        switch (platform) {
            case QQ:
                sharePt = "QQ";
                break;
            case QZONE:
                sharePt = "QZONE";
                break;
            case SINA:
                sharePt = "SINA";
                break;
            case WEIXIN:
                sharePt = "WEIXIN";
                break;
            case WEIXIN_CIRCLE:
                sharePt = "WEIXIN_CIRCLE";
                break;
            case SMS:
                sharePt = "SMS";
                break;
            case EMAIL:
                sharePt = "EMAIL";
                break;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("id", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("planid", "");
        map.put("pt", sharePt);
        map.put("obj", "");
        mPresenter.loadData(RetrofitManager.getInstance().saveFxInfo(map), Link.APP_SAVE_FX_INFO);
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        ToastUtil.showShort(this, "分享失败，" + throwable.getMessage());
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        ToastUtil.showShort(this, "分享取消");
    }
}
